  
package com.xebest.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.form.XeVenStoreRoleForm;
import com.xebest.web.mapper.write.XeVenStoreInfoWriteMapper;
import com.xebest.web.mapper.write.XeVenStoreRoleWriteMapper;
import com.xebest.web.model.XeVenStoreRoleEntity;
import com.xebest.web.service.XeVenStoreRoleService;
import com.xebest.web.view.XeVenStoreRoleView;

@Service
public class XeVenStoreRoleServiceImpl implements XeVenStoreRoleService {

    private static final Logger log = LoggerFactory.getLogger(XeVenStoreRoleServiceImpl.class);//日志记录类

    @Autowired
    private XeVenStoreRoleWriteMapper xeVenStoreRoleWriteMapper; //仓库角色权限业务操作
    @Autowired
    private XeVenStoreInfoWriteMapper storeInfoWriteMapper;//仓库写Mapper层
	/**
     * 根据用户标识查找已经分配的角色仓库
     * @author: 李贝贝
     * @createTime: 2017年6月17日 上午11:54:09 
     * @modifyTime: 2017年9月 11日 下午  dcj 
     * @history:  
     * @param eRoleEntity
     * @return List<Map<String, Object>>
     */
     public  List<Map<String, Object>> queryRoleStore(XeVenStoreRoleForm form){
    	 XeVenStoreRoleEntity entity = SysSetBeanUtils.toStoreRoleEntity(form);//实体转换
    	 log.debug("根据用户标识查找已经分配的角色仓库实体转换为{}",JSON.toJSONString(entity));
    	 List<XeVenStoreRoleView> storeRoleViewList = new ArrayList<XeVenStoreRoleView>();
    	 //查询出改角色拥有的区域权限
    	 storeRoleViewList = xeVenStoreRoleWriteMapper.queryRoleStore(entity);
    	 Map<String, Object> roleStoreMap = new HashMap<String, Object>();
    	 if(null != storeRoleViewList && storeRoleViewList.size() > 0){
    		 for(XeVenStoreRoleView srView : storeRoleViewList){
    			 roleStoreMap.put(srView.getStoreId(), srView.getStoreId());
    		 }
    	 }
    	 //查询出该商户下的仓库列表
    	 Map<String,Object> param = new HashMap<String, Object>();
    	 param.put("userCode", form.getUserCode());
    	 List<Map<String, Object>> storeInfoList = new ArrayList<Map<String, Object>>();
    	 storeInfoList = storeInfoWriteMapper.listStoreNameByParam(param);
    	 if(null != storeInfoList && storeInfoList.size() > 0){
    		 for(Map<String, Object> storeInfo : storeInfoList){
    			 String StoreId = storeInfo.get("storeId").toString();
    			 if(roleStoreMap.get(StoreId) != null){
    				 storeInfo.put("checkedFlag", true);
    			 } else {
    				 storeInfo.put("checkedFlag", false);
    			 }
    		 }
    	 }
    	 return storeInfoList;
     }

    /**
     * 批量插入角色仓库信息
     * @author: 李贝贝
     * @createTime: 2017年6月17日 下午12:18:37  
     * @history:  
     * @param list
     * @return int
     */
     public  int insertStoreRole(XeVenStoreRoleForm form) throws Exception{
    	 List<XeVenStoreRoleEntity> paramEntites = SysSetBeanUtils.toStoreRoleList(form);//转换要保存的角色仓库权限
    	 log.debug("批量插入角色仓库信息实体转换为{}",JSON.toJSONString(paramEntites));
    	 return xeVenStoreRoleWriteMapper.insertStoreRole(paramEntites);
     }
    

     /**
      * 修改区域角色权限 策略：先删除再添加
      * @author: 李贝贝
      * @createTime: 2017年6月17日 下午2:27:34  
      * @history:  
      * @param storeRoleForm
      * @return
      * @throws Exception int
      */
     @Transactional(value="writeTransactionManager")
     public  int updateStoreRole(XeVenStoreRoleForm form) throws Exception{
    	 XeVenStoreRoleEntity entity = SysSetBeanUtils.toStoreRoleEntity(form);
    	 log.debug("修改区域角色权限 策略：先删除再添加实体转换为：{}",JSON.toJSONString(entity));
    	 xeVenStoreRoleWriteMapper.deleteStoreRole(entity);//删除角色仓库权限
    	 List<XeVenStoreRoleEntity> paramEntites = SysSetBeanUtils.toStoreRoleList(form);//转换要保存的角色仓库权限
    	 log.debug("修改区域角色权限 策略：先删除再添加实体转换为：{}",JSON.toJSONString(paramEntites));
    	 return xeVenStoreRoleWriteMapper.insertStoreRole(paramEntites);
     }
}
  
